//
//  HOAppraiserTableViewCell.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 2/12/16.
//  Copyright © 2016 ShenOu. All rights reserved.
//

#import "HOAppraiserTableViewCell.h"
#import "RatingBar.h"
#import "HOUtils.h"
#import "HOImageMgr.h"
#import "UIImageView+WebCache.h"
#import "HOButton.h"

@interface HOAppraiserTableViewCell()
{
	UIImageView			*headerImgView;
	UILabel 			*workStatusLbl;
	UILabel				*displayNameLbl;
	UILabel				*contentLbl;
	UILabel				*timeLbl;
	UILabel				*departmentLbl;
	RatingBar			*ratingBar;
	BOOL				isInited;
	
	UIView				*showAllView;
	UIButton			*complaintBtn;
	int 				appraiseId;
	CGFloat 			height;
}

@end
@implementation HOAppraiserTableViewCell
@synthesize deleagte;
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)initWithWidth:(CGFloat)width
{
	self.cellWidth = width;
	[self initViews];
	[self initShowAllBtn];
}

- (void)initShowAllBtn
{
	showAllView = [[UIView alloc]
					 initWithFrame:CGRectMake(0,
											  0,
											  self.cellWidth,
											  56)];
	[showAllView setBackgroundColor:[UIColor whiteColor]];
	[showAllView.layer setMasksToBounds:YES];
	HOButton *btn = [[HOButton alloc]
					 initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
											  HO_HORIZONAL_PADDING,
											  self.cellWidth - 2 * HO_HORIZONAL_PADDING,
											  36)];
	[btn setStyleWithTitle:NSLocalizedString(@"查看全部评价", @"查看全部评价")
				titleColor:UIColorFromRGB(0xb4b4b4)
				 titleFont:[UIFont systemFontOfSize:14]
				norBgColor:[UIColor whiteColor]
				selBgColor:TEXT_GRAY_COLOR
		   highlighBgColor:TEXT_GRAY_COLOR
				  selector:@selector(viewAllAppraise:)
					target:self];
	btn.layer.borderWidth = 1;
	btn.layer.borderColor = UIColorFromRGB(0xcdcdcd).CGColor;
	[showAllView addSubview:btn];
	[btn release];
	[showAllView.layer setMasksToBounds:YES];
	showAllView.hidden = YES;
	[self.contentView addSubview:showAllView];
}

- (void)viewAllAppraise:(id)sender
{
	if(self.deleagte && [self.deleagte respondsToSelector:@selector(showAllAppraise:)])
	{
		[self.deleagte showAllAppraise:sender];
	}
}

- (void)initViews
{
	if(isInited)
		return;
	isInited = YES;
	[self initheaderImgView];
	[self initDisplayNameLbl];
	[self initRatingBar];
	[self initDepartmentLbl];
	[self initContentLbl];
	[self initTimeLbl];
//	[self initComplaintBtn];
}

- (void)dealloc
{
	[HOUtils removeFromSuperViewForViews:headerImgView,
										displayNameLbl,
										contentLbl,
										timeLbl,
										ratingBar,
										showAllView];
	[headerImgView release];
	[displayNameLbl release];
	[contentLbl release];
	[timeLbl release];
	[ratingBar release];
	[departmentLbl release];
	[showAllView release];
	if(complaintBtn)
	{
		[HOUtils removeFromSuperViewForViews:complaintBtn];
		[complaintBtn release];
	}
	self.deleagte = nil;
	[super dealloc];
}

- (void)initheaderImgView
{
	headerImgView = [[UIImageView alloc]
			   initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
										15,
										41,
										41)];
	headerImgView.layer.cornerRadius = 20.5;
	[headerImgView.layer setMasksToBounds:YES];
	[self.contentView addSubview:headerImgView];
//	headerImgView.layer.borderWidth = 1;
	
	
	workStatusLbl = [[[UILabel alloc]
					  initWithFrame:CGRectMake(12,
											   60,
											   40,
											   22)] autorelease];
	[self.contentView addSubview:workStatusLbl];
	workStatusLbl.textColor = TEXT_COLOR_181818;
	workStatusLbl.font = [UIFont systemFontOfSize:12];
}

- (void)initDisplayNameLbl
{
	CGFloat x = headerImgView.frame.size.width + 3 * HO_HORIZONAL_PADDING;
	displayNameLbl = [[UILabel alloc]
					  initWithFrame:CGRectMake(x,
											   15,
											   self.cellWidth - x - 100,
											   22)];
	displayNameLbl.font = [UIFont systemFontOfSize:14];
//	displayNameLbl.layer.borderWidth = 1;
	displayNameLbl.textColor = TEXT_COLOR_666666;
	[self.contentView addSubview:displayNameLbl];
}

- (void)initRatingBar
{
	ratingBar = [[RatingBar alloc]
				 initWithFrame:CGRectMake(displayNameLbl.frame.origin.x,
										  displayNameLbl.frame.origin.y + displayNameLbl.frame.size.height + 4,
										  displayNameLbl.frame.size.width,
										  33) isSmall:NO];
//	ratingBar.layer.borderWidth = 1;
	[self.contentView addSubview:ratingBar];
}


- (void)initDepartmentLbl
{
	CGFloat x = headerImgView.frame.size.width + 3 * HO_HORIZONAL_PADDING;
	departmentLbl = [[UILabel alloc]
					 initWithFrame:CGRectMake(x,
											  ratingBar.frame.origin.y + ratingBar.frame.size.height + 3,
											   self.cellWidth - x - 100,
											   22)];
	departmentLbl.font = [UIFont systemFontOfSize:14];
//	departmentLbl.layer.borderWidth = 1;
	departmentLbl.textColor = TEXT_COLOR_666666;
	[self.contentView addSubview:departmentLbl];
}
- (void)initContentLbl
{
	contentLbl = [[UILabel alloc]
				initWithFrame:CGRectMake(departmentLbl.frame.origin.x,
										 departmentLbl.frame.origin.y + departmentLbl.frame.size.height + 3,
										 self.cellWidth - displayNameLbl.frame.origin.x - HO_HORIZONAL_PADDING,
										 100)];
	contentLbl.textColor = TEXT_GRAY_DARK_COLOR;
	contentLbl.numberOfLines = 0;
//	contentLbl.layer.borderWidth = 1;
	contentLbl.font = displayNameLbl.font;
	[self.contentView addSubview:contentLbl];
}
- (void)initTimeLbl
{
	timeLbl = [[UILabel alloc]
				  initWithFrame:CGRectMake(self.cellWidth - 140 - HO_HORIZONAL_PADDING,
										   displayNameLbl.frame.origin.y,
										   140,
										   22)];
	timeLbl.font = [UIFont systemFontOfSize:12];
	timeLbl.textAlignment = NSTextAlignmentRight;
	timeLbl.textColor = TEXT_COLOR_999999;
//	timeLbl.layer.borderWidth = 1;
	[self.contentView addSubview:timeLbl];
}

- (void)initComplaintBtn
{
	complaintBtn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
	complaintBtn.frame = CGRectMake(self.cellWidth - 40 - HO_HORIZONAL_PADDING,
									displayNameLbl.frame.origin.y,
									40,
									20);
//	complaintBtn.layer.borderWidth = 1;
	complaintBtn.layer.borderColor = TEXT_GRAY_DARK_dbdbdb.CGColor;
	complaintBtn.layer.cornerRadius = 3;
	complaintBtn.titleLabel.font = [UIFont systemFontOfSize:12];
	[complaintBtn setTitleColor:TEXT_GRAY_COLOR forState:UIControlStateNormal];
	[complaintBtn setTitleColor:TEXT_GRAY_DARK_COLOR forState:UIControlStateSelected];
	[complaintBtn setTitleColor:TEXT_GRAY_DARK_COLOR forState:UIControlStateHighlighted];
	[complaintBtn setTitle:NSLocalizedString(@"举报", @"举报") forState:UIControlStateNormal];
	[self.contentView addSubview:complaintBtn];
	[complaintBtn addTarget:self action:@selector(complaint:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)complaint:(id)sender
{
	if(self.deleagte && [self.deleagte respondsToSelector:@selector(sendComplaint:)])
	{
		[self.deleagte sendComplaint:appraiseId];
	}
}

- (void)needShowAllBtn
{
	showAllView.hidden = NO;
}

- (void)setEntity:(HOAppraiseEntity *)entity
{
	timeLbl.text = [HOUtils getDateFromTimeInterval4:entity.time];
	[headerImgView sd_setImageWithURL:[NSURL URLWithString:entity.headPic] placeholderImage:[[HOImageMgr sharedInstance] getDefaultImage]];
	displayNameLbl.text = [NSString stringWithFormat:@"%@-%@", [HOUtils getValideString:entity.workId], [HOUtils getValideString:entity.userName]];
	ratingBar.starNumber = entity.value;
	departmentLbl.text = [NSString stringWithFormat:@"%@%@", NSLocalizedString(@"科室：", @"科室："), [HOUtils getValideString:entity.department]];
//	contentLbl.layer.borderWidth = 1;
	timeLbl.text = [HOUtils getDateFromTimeInterval:entity.time * 1000];
	contentLbl.frame = CGRectMake(departmentLbl.frame.origin.x,
								  departmentLbl.frame.origin.y + departmentLbl.frame.size.height + 3,
								  self.cellWidth - displayNameLbl.frame.origin.x - HO_HORIZONAL_PADDING,
								  100);
	if([HOUtils getValideString:entity.content].length > 0)
	{
		contentLbl.text = [HOUtils getValideString:entity.content];
		[contentLbl sizeToFit];
		contentLbl.frame = CGRectMake(contentLbl.frame.origin.x,
									  contentLbl.frame.origin.y,
									  contentLbl.frame.size.width,
									  MAX(contentLbl.frame.size.height, 22));
		
		height = contentLbl.frame.size.height + contentLbl.frame.origin.y + HO_HORIZONAL_PADDING;
	}
	else
	{
		contentLbl.text = @"";
		height = departmentLbl.frame.size.height + departmentLbl.frame.origin.y + HO_HORIZONAL_PADDING;
	}
	

//	if(entity.isWorking)
//	{
//		workStatusLbl.text = @"[在线]";
//		workStatusLbl.textColor = UIColorFromRGB(0x0b7f1e);
//	}
//	else
//	{
//		workStatusLbl.text = @"[离线]";
//		workStatusLbl.textColor = UIColorFromRGB(0xcdcdcd);
//	}
}

- (CGFloat)getCellHeight
{
	return height;
}

@end
